package edu.intprog.configuration;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import edu.intprog.egreeting.beans.Greeting;

/**
 * ConfigurationManager
 * 
 * This class is used for the general purpose of filling some commonly used
 * attributes to requests (for example the background music name<->file uri)
 */
public class ConfigurationManager {
	public final static String[] propFolderNames = {"BackgroundImages",
		"ForegroundImages",
		"Music"};
	
	public final static String[] propParamNames = {"backImages",
		"foreImages",
		"backMusics"};
	
	private static Properties props = null;
	
	private ConfigurationManager(HttpServletRequest request) {
		// no one should make objects of this class
	}
	
	/**
	 * loadPropertiesFromFile
	 * 
	 * this function will load a properties file (mapping.properties) from a folder that
	 * is named 'whatToLoad' and is under the Data folder 
	 * 
	 * @param servletRealPath - the real webapp path
	 * @param whatToLoad - the folder name which contains mapping.properties 
	 * (folder is under the Data folder)
	 * @return
	 */
	private static Properties loadPropertiesFromFile(String servletRealPath, String whatToLoad)
	{
		Properties prop = new Properties();
		String propFilePath = servletRealPath + File.separator +"Data" + File.separator + whatToLoad + File.separator + "mapping.properties";
		try {
			FileInputStream propFileIS = new FileInputStream(propFilePath);
			prop.load(propFileIS);
		} catch (Exception e) {
	
		}
		return prop;
	}
	
	/**
	 * fillRequestWithGreetingEssentials
	 * 
	 * add the foreground images, background images and music mappings (as Properties)
	 * as attributes into the given request 
	 * 
	 * @param request - request that should contain the listed above
	 */
	public static synchronized void fillRequestWithGreetingEssentials(HttpServletRequest request) {
		if (props==null)
		{
			String servletRealPath = request.getSession().getServletContext().getRealPath("/");
			props = new Properties();
			for (int i=0;i<propFolderNames.length;++i)
			{
				props.put(propFolderNames[i], loadPropertiesFromFile(servletRealPath, propFolderNames[i]));
			}
		}
		
		for (int i=0;i<propFolderNames.length;++i)
		{
			request.setAttribute(propParamNames[i], props.get(propFolderNames[i]));
		}
	}
	
	/**
	 * getGreetingEssentialProperty
	 * 
	 * @param propName - propety mapping name to return - should be one of
	 * 'ConfigurationManager.propFolderNames'
	 * @return - Properties mapping
	 */
	public static synchronized Properties getGreetingEssentialProperty(String propName) {
		return (Properties) props.get(propName);
	}
	
	/**
	 * prepareGreetingForPreview
	 * 
	 * this function preparing the given greeting to be previewed
	 * 
	 * @param greeting - greeting to preview
	 * @param request - servlet request
	 */
	public static synchronized void prepareGreetingForPreview(Greeting greeting, HttpServletRequest request) {
		int i = 0;

		//load the selected music and images to the request
		Properties backImages = ConfigurationManager.getGreetingEssentialProperty(ConfigurationManager.propFolderNames[i++]);
		String bImage = (String) backImages.get(greeting.get_backgroundImage());
		request.setAttribute("bImage",bImage);

		Properties foreImages = ConfigurationManager.getGreetingEssentialProperty(ConfigurationManager.propFolderNames[i++]);
		String fImage = (String) foreImages.get(greeting.get_greetingImage());
		request.setAttribute("fImage", fImage);

		Properties backMusic = ConfigurationManager.getGreetingEssentialProperty(ConfigurationManager.propFolderNames[i++]);
		String bMusic = (String) backMusic.get(greeting.get_backgroundSound());
		request.setAttribute("backMusic", bMusic);

		request.setAttribute("greeting", greeting);
	}
}
